package com.cn.gmall.weball.controller;

import com.cn.gmall.common.result.Result;
import com.cn.gmall.list.client.ListFeignClient;
import com.cn.gmall.model.list.SearchParam;

import org.apache.commons.lang.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@SuppressWarnings("all")
public class ListController {
    @Autowired
    ListFeignClient listFeignClient;

    @GetMapping("list.html")
    public String search(SearchParam searchParam, Model model) {
        Result<Map<String, Object>> list = listFeignClient.list(searchParam);
//        页面需要urlParam 的拼接路径
        String urlParam = this.createUrlParam(searchParam);

//        品牌面包屑     品牌:小米
        String trademarkParam = this.createTrademarkParam(searchParam);

//        品牌属性 面包屑
        List<Map<String, String>> proposList = this.createPropsParamList(searchParam.getProps());

//        排序orderMap
        Map<String, Object> orderMap = this.createOrderMap(searchParam.getOrder());
        model.addAttribute("orderMap",orderMap);
        model.addAttribute("propsParamList", proposList);
        model.addAttribute("trademarkParam", trademarkParam);
        model.addAttribute("urlParam", urlParam);
        model.addAttribute("searchParam", searchParam);
        model.addAllAttributes(list.getData());
        return "list/index";
    }

    /**
     * 排序orderMap
     * <p>
     * 1:asc   ==hotScore:ASC
     * 2:asc == Price:ASC
     *
     * @param order
     * @return
     */
    private Map<String, Object> createOrderMap(String order) {
        HashMap<String, Object> orderMap = new HashMap<>();

        if (!StringUtils.isEmpty(order)) {
//            order= 1:asc
            String[] split = order.split(":");
            if (!ArrayUtils.isEmpty(split) && split.length == 2) {

//                若平台点击了传值
                orderMap.put("type", split[0]);
                orderMap.put("sort", split[1]);
            }
        } else {
//                默认排序规则
            orderMap.put("type", "1");
            orderMap.put("sort", "desc");

        }

        return orderMap;
    }

    /***
     * 平台属性面包屑
     * @param props
     * @return
     */
    private List<Map<String, String>> createPropsParamList(String[] props) {
        ArrayList<Map<String, String>> propsList = new ArrayList<>();
        if (!ArrayUtils.isEmpty(props)) {

            for (String prop : props) {
                //            prop = 23:6G:运行内存
                String[] split = prop.split(":");
                if (!ArrayUtils.isEmpty(split) && split.length == 3) {
                    HashMap<String, String> map = new HashMap<>();
                    map.put("attrId", split[0]);
                    map.put("attrValue", split[1]);
                    map.put("attrName", split[2]);
                    propsList.add(map);
                }
            }
        }
        return propsList;
    }

    /***
     * 品牌面包屑
     *
     * tradeMark = 1:小米
     * @param searchParam
     * @return
     */
    private String createTrademarkParam(SearchParam searchParam) {

        if (!StringUtils.isEmpty(searchParam.getTrademark())) {
            String[] split = searchParam.getTrademark().split(":");
            if (!ArrayUtils.isEmpty(split) && split.length == 2) {
                return "品牌:" + split[1];
            }

        }
        return "";
    }

    /**
     * 拼接urlParam路径
     * <p>
     * http://list.gmall.com/list.html?keyword=手机    ---格式一
     * http://list.gmall.com/list.html?category1Id=2  ---格式二
     *
     * @param searchParam
     * @return
     */
    private String createUrlParam(SearchParam searchParam) {

        StringBuilder urlParam = new StringBuilder();

        if (!StringUtils.isEmpty(searchParam.getKeyword())) {
//         http://list.gmall.com/list.html?keyword=手机    ---格式一
            urlParam.append("keyword=").append(searchParam.getKeyword());
        }

        if (searchParam.getCategory1Id() != null) {
            urlParam.append("&category1Id=").append(searchParam.getCategory1Id());
        }
        if (searchParam.getCategory2Id() != null) {
            urlParam.append("&category2Id=").append(searchParam.getCategory2Id());
        }
        if (searchParam.getCategory3Id() != null) {
            urlParam.append("&category3Id=").append(searchParam.getCategory3Id());
        }

//           品牌 参数
        if (!StringUtils.isEmpty(searchParam.getTrademark())) {
//            这个参数前面一定会有其他参数例如keyWord或category1Id
            if (urlParam.length() > 0) {
                urlParam.append("&trademark=").append(searchParam.getTrademark());
            }
        }
//                  平台属性
        if (!ArrayUtils.isEmpty(searchParam.getProps())) {

            for (String prop : searchParam.getProps()) {
                urlParam.append("&props=").append(prop);
            }
        }


        return "list.html?" + urlParam.toString();
    }


}
